package com.wh.wisdomsite.xbox.ba.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.dao.RegionMapper;
import com.wh.wisdomsite.xbox.ba.entity.Region;
import com.wh.wisdomsite.xbox.ba.service.IRegionService;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 公共区域ServiceImpl
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.ba.service.impl
 * @date 2018年8月3日 10:03
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司CopyRight (c) 2018
 */
@Service("baRegionService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    private static final Log logger = LogFactory.getLog(RegionServiceImpl.class);

    @Autowired
    private RegionMapper regionMapper;


    /**
     * 获取区域Map结构
     * @param key
     * @return
     */
    @Override
    public LinkedHashMap<String, List<String>> getRegionMap(String key) {
        LinkedHashMap<String, List<String>> RegionMap = new LinkedHashMap<>();
        List<Region> Regions =  regionMapper.getRegionMap(key);
        if(null != Regions && Regions.size() > 0){
            //区域集合
            List<String> listMap = new ArrayList<>();
            //港澳台集合
            List<String> katMap = new ArrayList<>();
            //记录上一次codeId
            String lastCodeId = null;
            //记录上一次province
            String lastProvince = null;
            for(int i = 0; i < Regions.size(); i++ ){
                Region Region = Regions.get(i);
                String codeId = Region.getCodeId();
                String parentId = Region.getParentId();
                String city = Region.getCity();
                String province = Region.getProvince();
                /**
                 * 1. 过滤香港，台湾，澳门等城市
                 */
                if(codeId.equals("710000") || codeId.equals("810000") || codeId.equals("820000") || parentId.equals("710000") || parentId.equals("810000") || parentId.equals("820000") ){
                    if(StringUtils.isNotEmpty(city)){
                        katMap.add(city);
                    }
                    continue;
                }

                /**
                 * 2. 处理区域城市
                 */
                if("0".equals(parentId)){
                    if(StringUtils.isNotEmpty(lastCodeId) && StringUtils.isNotEmpty(lastProvince)){
                        RegionMap.put(lastProvince, listMap);
                    }
                    lastCodeId = codeId;
                    lastProvince = province;
                    listMap = new ArrayList<>();
                    continue;
                }
                //表示城市节点
                if(!"0".equals(parentId)){
                    if(parentId.equals(lastCodeId)){
                        listMap.add(city);
                    }
                    if(i == Regions.size() - 1){
                        if(StringUtils.isNotEmpty(lastCodeId) && StringUtils.isNotEmpty(lastProvince)){
                            RegionMap.put(lastProvince, listMap);
                        }
                    }
                }
            }

            /**
             * 3. 删除直辖市
             */
            RegionMap.remove("北京");
            RegionMap.remove("天津");
            RegionMap.remove("上海");
            RegionMap.remove("重庆");
            List<String> zxsList = new ArrayList<>();
            zxsList.add("北京");
            zxsList.add("天津");
            zxsList.add("上海");
            zxsList.add("重庆");
            RegionMap.put("直辖市", zxsList);

            /**
             * 4. 添加港澳台
             */
            RegionMap.put("港澳台", katMap);

        }
        return RegionMap;
    }


    /**
     * 获取热门城市
     * @return
     */
    @Override
    public List<String> getHotCityList() {
        List<Region> Regions =  regionMapper.getHotCityList();
        List<String> hotCitys = new ArrayList<>();
        for(Region Region :  Regions){
            hotCitys.add(Region.getCity());
        }
        return hotCitys;
    }


    /**
     * 根据codeIds获取城市信息
     * @param codeIds
     * @return
     */
    @Override
    public List<Region> getRegionByCodeIds(List<Integer> codeIds) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("codeIds", codeIds);
        return regionMapper.getRegionByCodeIds(map);
    }


    /**
     * 根据城市regionId获取省级对象
     * @param regionId
     * @return
     */
    @Override
    public Region getRegionParentByRegionId(Integer regionId) {
        return  regionMapper.getRegionParentByRegionId(regionId);
    }


    @Override
    public List<Region> getProvinceList(String provinceId) {
        return regionMapper.getProvinceList(provinceId);
    }

    @Override
    public List<Region> getCityList(String provinceId) {
        return regionMapper.getCityList(provinceId);
    }

    @Override
    public List<Region> getDistrictList(String cityId) {
        return regionMapper.getDistrictList(cityId);
    }

}
